# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import os,csv
from scrapy.exporters import CsvItemExporter
class TongchengPipeline:
    def open_spider(self, spider):
        print("开始输出CSV文件")
        self.file = open("./info.csv", "a+",newline='',encoding="utf-8")
        self.writer = csv.writer(self.file)
        self.writer.writerow(['id','sex','marital_status','age','education','area','job','height','company_type',
                              'weight','month_avg_income','horoscope','apartment','animal','vehicle','pet_name','self_introduction','search'])
    def process_item(self, item, spider):
        line = []
        for i in item:
            if(item[i]==''):
                item[i]="未透露"
            line.append(item[i])
        self.writer.writerow(line)
        return item

    def close_spider(self, spider):
        self.file.close()
        print("结束输出CSV文件")
